// import Vue from 'vue';
import Mint from 'mint-ui';
import 'mint-ui/lib/style.min.css'
import 'lib-flexible/flexible.js';
import 'url-search-params-polyfill';
import "@babel/polyfill";
import wx from 'weixin-js-sdk';
import App from './App';
import router from './router';
import store from './store';
import { shopInfo,userInfo } from '@/api/user'
Vue.config.productionTip = false;
Vue.use(Mint);
Vue.prototype.$wx = wx;


router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  const localToken=localStorage.getItem('token');
  /**判断该路由是否需要登录**/
  if(to.meta.requireAuth){

    /**判断本地token是否存在**/
    if(localToken){
      /**判断本地用户信息是否存在**/
      if(sessionStorage.getItem('userName')){
        next();
      }else{
        /**获取店铺信息**/
        shopInfo().then(res => {
          sessionStorage.setItem('currentName',res.object.shopName); // 保存currentName
          sessionStorage.setItem('storeType',res.object.storeType); // 保存storeType

          /**获取用户信息**/
          userInfo().then(resUser => {
            sessionStorage.setItem('userName',resUser.object.name); // 保存userName
            sessionStorage.setItem('isAdmin',resUser.object.isAdmin); // 保存isAdmin
            sessionStorage.setItem('sShopKeeper',resUser.object.isShopKeeper); // 保存isShopKeeper
            sessionStorage.setItem('linkPhone',resUser.object.code); // 保存linkPhone

            next();
          }).catch(error => {
            console.log(error,111)
          })
        }).catch(error => {
          console.log(error,111)
        })
      }

    }else{
      console.log(111)
      router.push({path:'/login'})
      next();
    }
  }else{
    next();
  }
});
// 跳转后返回顶部
router.afterEach((to,from,next) => {
  window.scrollTo(0,0);
})
new Vue({
  el: '#app',
  store,
  router,
  components: { App },
  template: '<App/>',
});
